package com.gao.web.controller;

import com.gao.web.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class HouseController extends HttpServlet {
    @Autowired
    HouseService houseService;

    @RequestMapping("/inserthouse")
    public String insert(MultipartFile house_type_diagram, String house_type, String build_area, String inside_area, String address, String price, Model model) {
        String post_date = String.valueOf(System.currentTimeMillis() / 1000);
        String uploadPath = "D:/maven/project/柒鸽房屋中介/src/main/webapp/images/" + house_type_diagram.getOriginalFilename();
        try (OutputStream os = new FileOutputStream(uploadPath)) {
            os.write(house_type_diagram.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String house_type_diagram1 = "/images/" + house_type_diagram.getOriginalFilename();
        int i = houseService.insertHouse(house_type, build_area, inside_area, address, house_type_diagram1, price, post_date);
        if (i == 1) {
            model.addAttribute("e", "1");
            return "/addHouse.jsp";
        } else {
            model.addAttribute("e", "0");
            return "/addHouse.jsp";
        }
    }

    @RequestMapping("/deletehouse")
    public String delete(String id, Model model) {
        int Id = Integer.parseInt(id);
        int i = houseService.delete(Id);
        if (i == 1) {
            model.addAttribute("data", "0");
            return "house_info.jsp";
        } else {
            model.addAttribute("data", "1");
            return "house_info.jsp";
        }
    }
}
